const cloud = require("@cloudbase/node-sdk");

const app = cloud.init({
    env: cloud.SYMBOL_CURRENT_ENV,
});

var db = app.database();
const _ = db.command
const ZXClan = db.collection('ZX-Clan')
const ZXClanScore = db.collection('ZX-Clan-Score')

const nowDate = db.serverDate()
// 获取openId云函数入口函数
exports.main = async (event, context) => {
    process.env.TZ = 'Asia/Shanghai'
    api = event.api

    if(api=='api-update-all'){
        // var _id = event.data._id
        // delete event.data._id
        // return await ZXClan.doc(_id).update(event.data)
        return await ZXClan.where({}).update(event.data)
    }

    if(api=='api-clan-add'){
        params = {}
        var name = event.data.name
        var _id = event.data._id
        if(name)
            params.name = name
        if(_id){
            delete event.data._id
            return await ZXClan.doc(_id).update(event.data)
        }else{
            const total = (await ZXClan.where(params).count()).total
            // return total
            if(total){
                return {
                    type:'error',
                    msg:'名字已存在',
                }
            }else{
                return await ZXClan.add(event.data)
            }
        }
    }

    if(api=='api-clan-del'){
        var _id = event.data._id
        if(_id){
            return await ZXClan.doc(_id).remove()
        }
    }

    if(api=='api-clan-list'){
        params = {}
        nameArr = event.nameArr
        idsArr = event.idsArr
        var name = event.data.name
        var house = event.data.house
        var cianType = event.data.cianType
        if (name)
            params.name = db.RegExp({
                regexp: String(name),
                options: 'i',
            })
        if(nameArr)
            params.nameArr = _.or([{name: _.in(nameArr)}])
        if(idsArr)
            params.idsArr = _.or([{_id: _.in(idsArr)}])
        if(house)
            params.house = house
        if(cianType)
            params.cianType = cianType

        const res = await db
        .collection('ZX-Clan')
        .aggregate()
        .match(params)
        .sort({
            house: 1,
            sect: 1,
        })
        .skip((event.data.pageIndex - 1) * event.data.pageSize)
        .limit(event.data.pageSize)
        .end()

        const list = res.data
        const total = (await ZXClan.where(params).count()).total

        return {
            list: list,
            total: total,
        }

    }

    if(api=='api-clanscore-add'){
        event.data.crearTime = nowDate
        return await ZXClanScore.add(event.data)
    }

    if(api=='api-clanscore-list'){
        params = {}
        
        const res = await db
        .collection('ZX-Clan-Score')
        .aggregate()
        .match(params)
        .sort({
            crearTime: -1,
        })
        .end()
        return res

    }


    if(api=='api-houser-list'){
        const houserItem  = [
            {label: "*狂*飙*", value: "1",},
            {label: "不败&神话", value: "2",},
            {label: "你动我一下试试", value: "3",},
            {label: "青云门", value: "4",},
            {label: "寒江孤影", value: "5",},
            {label: "凤兮凤兮归故乡", value: "6",},
            {label: "专治各种不服", value: "7",},
            {label: "听风小筑", value: "8",},
            {label: "仗剑游天下", value: "9",},
            {label: "天上白玉京", value: "10",},
            {label: "温柔乡", value: "11",},
            {label: "万花丛中过", value: "12",},
            {label: "帅到自然醒", value: "13",},
            {label: "落英听谁细数", value: "14",},
        ]
        return houserItem
    }



};